<?php
namespace app\admin\controller;
use think\Session;
use think\Request;
use app\admin\model\StaffsModel as M;
class Users extends Base{
    //修改自己的密码
    public function upPwd(Request $request)
    {
        $this->isLogin();
        if ($request->isAjax()) {
            //获取数据核对密码
            $m =  new M();
            $staffId = (int)(Session::get('STAFF.staff_id'));
            $res = $m->editPass($staffId);
            return $res;
        } else {
            $login_name = Session::get('STAFF.login_name');
            $this->assign('login_name',$login_name);
            return $this->fetch('change_pwd');
        }
    }
    //用户删除
    public function usersDel(){
        $this->isLogin();
        $staff_id = input('staff_id');
        $m = new M();
        $res = $m->del($staff_id);
        return $res;
    }
    //更改用户状态是否有权限登录
    public function setStatus(){
        $this->isLogin();
        $s = (int)input('s');
        $staff_id = input('id');
        $m = new M();
        if($s == 0){
            $res = $m->setStatus($staff_id,$s);
            return $res;
        }else{
            $res = $m->setStatus($staff_id,$s);
            return $res;
        }

    }

    //用户职员管理显示
    public function user(){
        $this->isLogin();
        $login_name = input('login_name');
        $m = new M();
        $res = $m->user($login_name);
        $this->assign('login_name',$login_name);
        $this->assign('users', $res);
        return $this->fetch('user_management');
    }

    //用户编辑
    public function userEdit(){
        $this->isLogin();
        if(Request::instance()->isPost()){
            $m = new M();
            $res = $m->toEdit();
            return $res;

        }else{
            $staff_id = input('id');
            $m = new M();
            $res = $m->userEdit($staff_id);
            $this->assign('users',$res);
            return $this->fetch('user_management_edit');
        }

    }
    //用户添加
    public function userAdd(){
        $this->isLogin();
        return $this->fetch('user_management_add');
    }
    //用户密码修改
    public function usersEditPwd(){
        $this->isLogin();
        if(Request::instance()->isPost()){
            $m = new M();
            $res = $m->usersEditPwd();
            return $res;
        }else{
            $m = new M();
            $res = $m->usersEditPwd();
            $this->assign('users',$res);
            return $this->fetch('user_management_password');
        }

    }
    //个人信息显示
    public function userDetail(){
        $this->isLogin();
        $staff_id = input('id');
        $m = new M();
        $res = $m->userDetail($staff_id);
        $this->assign('users',$res);
        return $this->fetch('user_management_show');
    }
    //批量删除
   public function del()
    {
        $this->isLogin();
        $id = input('ids');
        $staff_id = explode(',',$id);
        foreach($staff_id as $id){
            $m = new M();
            $res[] = $m->delAll($id);
        }
        return $res;
    }

}

